<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="rustdoc">
    <title>Native Windows GUI guide - Partials</title>

    <link rel="stylesheet" type="text/css" href="style/rustbook.css">
    <link rel="stylesheet" type="text/css" href="style/pygments.css">
    <link rel="stylesheet" type="text/css" href="style/nwg.css">


</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
    This old browser is unsupported and will most likely display funky
    things.
</div>
<![endif]-->

<!-- NAV BEGIN -->
<div id="nav">
    <button id="toggle-nav">
        <span class="sr-only">Toggle navigation</span>
        <span class="bar"></span>
        <span class="bar"></span>
        <span class="bar"></span>
    </button>
</div>

    
<div id='toc' class='mobile-hidden'>
<ul class='chapter'>
<li><a href='index.html'><b>1.</b> Introduction</a>
</li>
<li><a href='getting_started.html'><b>2.</b> Getting Started</a>
</li>

<li><a href="basics.html"><b>3.</b> Basics </a>
<ul class="section">
    <li><a href="controls.html"><b>3.1.</b> Controls </a></li>
    <li><a href="events.html"><b>3.2.</b> Events </a></li>
    <li><a href="helper.html"><b>3.3.</b> Helpers </a></li>
    <li><a href="small.html"><b>3.4.</b> Small application layout </a></li>
    <li><a href="limitations.html"><b>3.5.</b> Limitations </a></li>
    <li><a href="distribute.html"><b>3.6.</b> Distributing </a></li>
    <li><a href="features.html"><b>3.7.</b> Features </a></li>
</ul>
</li>

<li><a href="intermediate.html"><b>4.</b> Intermediate </a>
<ul class="section">
    <li><a href="layouts.html"><b>4.1.</b> Layouts </a></li>
    <li><a href="resources.html"><b>4.2.</b> Resources </a></li>
    <li><a href="dialogs.html"><b>4.3.</b> Dialogs </a></li>
    <li><a href="localization.html"><b>4.4.</b> Internationalization </a></li>
</ul>
</li>

<li><a href="advanced.html"><b>5.</b> Advanced </a>
<ul class="section">
    <li><a href="partial.html"><b>5.1.</b> Partials ui </a></li>
    <li><a href="dynamic_control.html"><b>5.2.</b> Dynamic control </a></li>
    <li><a href="dynamic_event.html"><b>5.3.</b> Dynamic events </a></li>
    <li><a href="multithreading.html"><b>5.4.</b> Multithreading </a></li>
</ul>
</li>

<li><a href="derive.html"><b>6.</b> Native-windows-derive </a>
<ul class="section">
    <li><a href="nwd_basics.html"><b>6.1.</b> Basics </a></li>
    <li><a href="nwd_controls.html"><b>6.1.</b> Controls </a></li>
    <li><a href="nwd_resources.html"><b>6.2.</b> Resources </a></li>
    <li><a href="nwd_events.html"><b>6.3.</b> Events </a></li>
    <li><a href="nwd_layouts.html"><b>6.4.</b> Layouts </a></li>
    <li><a href="nwd_partial.html"><b>6.5.</b> Partials </a></li>
</ul>
</li>

<li><a href="low.html"><b>7.</b> Low level stuff </a>
    <ul class="section">
        <li><a href="low_events.html"><b>7.1.</b> Raw event handling </a></li>
        <li><a href="extern_wrapping.html"><b>7.2.</b> Raw control handle </a></li>
    </ul>
</li>

</ul>
</div>
<!-- NAV END -->

<div id='page-wrapper'>
    <div id='page'>

        <h1 class="title">Native Windows GUI: Partials</h1>

        With small applications, controls, resources and layouts can usually fit within a single UI struct. That won't always be the case.
        Native windows GUI partials are a way to extend GUI beyond a single structure. <b>Partials are only a way to organize data; they do not use
        any kind of special methods.</b>
        <br/><br/>

        Partials are <b>self-contained</b>. That means they shouldn't have access to their parent structure data.
        <br/><br/>

        Partials are fully supported by <b>native-windows-derive</b>. See the <a href="nwd_partial.html">derive section</a> to learn how to use partial derive.
        <br/><br/>

        <h3>PartialUi</h3>

        Just like <code>NativeUi</code>, <code>PartialUi</code> is a trait that must be implemented on a UI struct. Here's the trait definition:<br><br>

<div class="highlight"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">PartialUi</span><span class="o">&lt;</span><span class="n">D</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">build_partial</span><span class="o">&lt;</span><span class="n">W</span>: <span class="nb">Into</span><span class="o">&lt;</span><span class="n">ControlHandle</span><span class="o">&gt;&gt;</span><span class="p">(</span><span class="n">d</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">D</span><span class="p">,</span><span class="w"> </span><span class="n">parent</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">W</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="n">NwgError</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">process_event</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_evt</span>: <span class="nc">Event</span><span class="p">,</span><span class="w"> </span><span class="n">_evt_data</span>: <span class="kp">&amp;</span><span class="nc">EventData</span><span class="p">,</span><span class="w"> </span><span class="n">_handle</span>: <span class="nc">ControlHandle</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">handles</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="n">ControlHandle</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">vec</span><span class="o">!</span><span class="p">[]</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
        <br>
        <b>build_partial</b><br><br>

        <code>build_partial</code> is the meat of the partial. It initialize the resources, the controls, and the layouts. Optionally you can send a parent control
        to this method. For partial without top level window, this parent control will be the root. <br><br>

        <b>process_event</b><br><br>

        <code>process_event</code> role is to process the NWG events. The method will be called inside a nwg callback and dispatch the callback
        the same way the event handlers works.<br><br>

        <b>handles</b><br><br>

        <code>handles</code> is the easiest method to implement. It's role is to return the handle to the top level windows of the partial.
        If the partial does not have any window, it should return an empty array.

        <h3>Implementing partials into NativeUi</h3>

        First, lets look at a basic application structure: <br>

<div class="highlight"><pre style="width: auto;"><span></span><span class="k">extern</span><span class="w"> </span><span class="k">crate</span><span class="w"> </span><span class="n">native_windows_gui</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">nwg</span><span class="p">;</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">nwg</span>::<span class="p">{</span><span class="n">NativeUi</span><span class="p">,</span><span class="w"> </span><span class="n">PartialUi</span><span class="p">};</span><span class="w"></span>

<span class="cp">#[derive(Default)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">MainUi</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">window</span>: <span class="nc">nwg</span>::<span class="n">Window</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">form</span>: <span class="nc">SubmitForm</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[derive(Default)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">SubmitForm</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">form_data</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">layout</span>: <span class="nc">nwg</span>::<span class="n">GridLayout</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">value</span>: <span class="nc">nwg</span>::<span class="n">TextInput</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">sumbit_button</span>: <span class="nc">nwg</span>::<span class="n">Button</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>

            
            
        <br>

        This example describes a basic application with a form. Forms are probably the best use case for UI partials.
        With this defined, it is time to implement the NWG UI traits.

        <br><br>

        <b>First</b>, lets implement <code>PartialUi</code>. Just because is simpler than the next step.

<div class="highlight"><pre><span></span><span class="k">impl</span><span class="w"> </span><span class="n">PartialUi</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">SubmitForm</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">build_partial</span><span class="o">&lt;</span><span class="n">W</span>: <span class="nb">Into</span><span class="o">&lt;</span><span class="n">nwg</span>::<span class="n">ControlHandle</span><span class="o">&gt;&gt;</span><span class="p">(</span><span class="n">data</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">SubmitForm</span><span class="p">,</span><span class="w"> </span><span class="n">parent</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">W</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="n">nwg</span>::<span class="n">NwgError</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">parent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parent</span><span class="p">.</span><span class="n">unwrap</span><span class="p">().</span><span class="n">into</span><span class="p">();</span><span class="w"></span>

<span class="w">        </span><span class="n">nwg</span>::<span class="n">TextInput</span>::<span class="n">builder</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">text</span><span class="p">(</span><span class="o">&amp;</span><span class="n">data</span><span class="p">.</span><span class="n">form_data</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">parent</span><span class="p">(</span><span class="o">&amp;</span><span class="n">parent</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">build</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">value</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="n">nwg</span>::<span class="n">Button</span>::<span class="n">builder</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">text</span><span class="p">(</span><span class="s">&quot;Save&quot;</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">parent</span><span class="p">(</span><span class="o">&amp;</span><span class="n">parent</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">build</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">sumbit_button</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="n">nwg</span>::<span class="n">GridLayout</span>::<span class="n">builder</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">child</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">.</span><span class="n">value</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">child</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">.</span><span class="n">sumbit_button</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">parent</span><span class="p">(</span><span class="o">&amp;</span><span class="n">parent</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">build</span><span class="p">(</span><span class="o">&amp;</span><span class="n">data</span><span class="p">.</span><span class="n">layout</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">process_event</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">evt</span>: <span class="nc">nwg</span>::<span class="n">Event</span><span class="p">,</span><span class="w"> </span><span class="n">_evt_data</span>: <span class="kp">&amp;</span><span class="nc">nwg</span>::<span class="n">EventData</span><span class="p">,</span><span class="w"> </span><span class="n">handle</span>: <span class="nc">nwg</span>::<span class="n">ControlHandle</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">use</span><span class="w"> </span><span class="n">nwg</span>::<span class="n">Event</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">E</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="n">evt</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">E</span>::<span class="n">OnButtonClick</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="o">&amp;</span><span class="n">handle</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">sumbit_button</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;PARTIAL EVENT!&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">},</span><span class="w"></span>
<span class="w">            </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">handles</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="n">nwg</span>::<span class="n">ControlHandle</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// No top level window in this partial</span>
<span class="w">        </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>

        <br><br>

        There's nothing new there. In fact it is simpler than implementing <code>NativeUi</code> because there's no event binding.

        <br><br>
       
        <b>Next</b>, it is time to implement <code>NwgUi</code> on <code>MainUi</code>. If you've already gone through <a href="small.html">small application layout</a>,
        this shouldn't be too hard:

        <br><br>

<div class="highlight"><pre style="width: auto;"><span></span><span class="c1">// ... Imports &amp; NwgUi definition ...</span>

<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">MainUiWrapper</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">inner</span>: <span class="nc">Rc</span><span class="o">&lt;</span><span class="n">MainUi</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">default_handler</span>: <span class="nc">RefCell</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">nwg</span>::<span class="n">EventHandler</span><span class="o">&gt;&gt;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">nwg</span>::<span class="n">NativeUi</span><span class="o">&lt;</span><span class="n">MainUiWrapper</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">MainUi</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">build_ui</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">data</span>: <span class="nc">MainUi</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="n">MainUiWrapper</span><span class="p">,</span><span class="w"> </span><span class="n">nwg</span>::<span class="n">NwgError</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">nwg</span>::<span class="n">Window</span>::<span class="n">builder</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">size</span><span class="p">((</span><span class="mi">500</span><span class="p">,</span><span class="w"> </span><span class="mi">500</span><span class="p">))</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">position</span><span class="p">((</span><span class="mi">500</span><span class="p">,</span><span class="w"> </span><span class="mi">300</span><span class="p">))</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">title</span><span class="p">(</span><span class="s">&quot;My Form&quot;</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">build</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">window</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="c1">// !!! Partials controls setup !!!</span>
<span class="w">        </span><span class="n">SubmitForm</span>::<span class="n">build_partial</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">form</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">data</span><span class="p">.</span><span class="n">window</span><span class="p">))</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">ui</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MainUiWrapper</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">inner</span>: <span class="nc">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="n">data</span><span class="p">),</span><span class="w"></span>
<span class="w">            </span><span class="n">default_handler</span>: <span class="nb">Default</span>::<span class="n">default</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="p">};</span><span class="w"></span>

<span class="w">        </span><span class="c1">// !!! Partials Event Binding !!!</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">window_handles</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="o">!</span><span class="p">[</span><span class="o">&amp;</span><span class="n">ui</span><span class="p">.</span><span class="n">window</span><span class="p">.</span><span class="n">handle</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="n">window_handles</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">ui</span><span class="p">.</span><span class="n">form</span><span class="p">.</span><span class="n">handles</span><span class="p">());</span><span class="w"></span>

<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="n">handle</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">window_handles</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">evt_ui</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">downgrade</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ui</span><span class="p">.</span><span class="n">inner</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">handle_events</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">|</span><span class="n">evt</span><span class="p">,</span><span class="w"> </span><span class="n">evt_data</span><span class="p">,</span><span class="w"> </span><span class="n">handle</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">use</span><span class="w"> </span><span class="n">nwg</span>::<span class="n">Event</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">E</span><span class="p">;</span><span class="w"></span>

<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">ui</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">evt_ui</span><span class="p">.</span><span class="n">upgrade</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="w">                    </span><span class="c1">// !!! Partials Event Dispatch !!!</span>
<span class="w">                    </span><span class="n">ui</span><span class="p">.</span><span class="n">form</span><span class="p">.</span><span class="n">process_event</span><span class="p">(</span><span class="n">evt</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">evt_data</span><span class="p">,</span><span class="w"> </span><span class="n">handle</span><span class="p">);</span><span class="w"></span>

<span class="w">                    </span><span class="k">match</span><span class="w"> </span><span class="n">evt</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="n">E</span>::<span class="n">OnButtonClick</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span>
<span class="w">                            </span><span class="k">if</span><span class="w"> </span><span class="o">&amp;</span><span class="n">handle</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">&amp;</span><span class="n">ui</span><span class="p">.</span><span class="n">form</span><span class="p">.</span><span class="n">sumbit_button</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                                </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;SAVING!&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                            </span><span class="p">},</span><span class="w"></span>
<span class="w">                        </span><span class="n">E</span>::<span class="n">OnWindowClose</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span>
<span class="w">                            </span><span class="k">if</span><span class="w"> </span><span class="o">&amp;</span><span class="n">handle</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">&amp;</span><span class="n">ui</span><span class="p">.</span><span class="n">window</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                                </span><span class="n">nwg</span>::<span class="n">stop_thread_dispatch</span><span class="p">();</span><span class="w"></span>
<span class="w">                            </span><span class="p">},</span><span class="w"></span>
<span class="w">                        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">                    </span><span class="p">}</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">};</span><span class="w"></span>

<span class="w">            </span><span class="n">ui</span><span class="p">.</span><span class="n">default_handler</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">().</span><span class="n">push</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="n">nwg</span>::<span class="n">full_bind_event_handler</span><span class="p">(</span><span class="n">handle</span><span class="p">,</span><span class="w"> </span><span class="n">handle_events</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">ui</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>


<span class="c1">// .. Drop &amp; Deref impl omitted </span>
</pre></div>
            
        <br>

        I've tagged the important bits where the new partial API is called.
        <br><br>

        <code>build_partial</code> is called just after the base control creation. This way the parent (if any) is sure to have been initialized.

        <br><br>

        <code>handles</code> is probably the most complex one. Because we must bind an event handler for each top level window, we begin by storing
        the handles of the main UI in a Vec, and then, for each partials, the <code>handles</code> method is called and the result is added to the vector.
        Finally, we iterator over the handles and bind the event handler on each one. The <code>EventHandler</code> are stored in the wrapper struct.

        <br><br>

        <code>process_event</code> is called in the event handler. The events parameters are forwarded to the function.<br>
        Note that we can also reference the partial control in the base handler using this code: <code>&handle == &ui.form.sumbit_button</code>.


        <h3>Examples</h3>

        <ul>
            <li><a href="https://github.com/gabdube/native-windows-gui/blob/master/native-windows-gui/examples/partials_simple.rs">Simple partial</a>. The final code of this tutorial</li>
            <li><a href="https://github.com/gabdube/native-windows-gui/blob/master/native-windows-gui/examples/partials.rs">Partials</a> ( <a href="https://github.com/gabdube/native-windows-gui/blob/master/native-windows-gui/examples/partials_d.rs">derive version</a> ) shows how to use partials.</li>
        </ul>
        <br/>

    </div>
</div>

<script src="style/rustbook.js"></script>
</body>
</html>
